package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.LoaiDanhMucDao;
import com.tt.fashionWebSite.domain.LoaiDanhMuc;
import com.tt.fashionWebSite.domain.MauSac;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.LoaiDanhMucDto;
import com.tt.fashionWebSite.dto.MauSacDto;
import com.tt.fashionWebSite.service.Ad_ProductListCategoryService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_productListCategoryService")
public class Ad_ProductListCategoryServiceImp extends GenericService implements Ad_ProductListCategoryService {
    @Autowired
    @Qualifier("loaiDanhMuc")
    private LoaiDanhMucDao loaiDanhMucDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductListCategory(LoaiDanhMucDto productListCategoryDto) {
        LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(productListCategoryDto.getMaLoai());
        if(loaiDanhMuc == null){
            loaiDanhMuc = new LoaiDanhMuc();
            loaiDanhMuc.setMaLoai(productListCategoryDto.getMaLoai());
            loaiDanhMuc.setTenLoai(productListCategoryDto.getTenLoai());
            loaiDanhMucDao.insert(loaiDanhMuc);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<LoaiDanhMucDto> getAllListCategory () {
        return mapper.mapCollection(loaiDanhMucDao.getAllProductCategory(), LoaiDanhMucDto.class);
    }

    public LoaiDanhMucDto getListCategoryInfoEdit(LoaiDanhMucDto categoryDto){
        LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(categoryDto.getMaLoai());
        LoaiDanhMucDto loaiDanhMucDao = mapper.map(loaiDanhMuc, LoaiDanhMucDto.class,LOAI_DANH_MUC_MAP_ID);
        return  loaiDanhMucDao;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public LoaiDanhMucDto registerListCategoryEdit(LoaiDanhMucDto categoryDto) {
        LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(categoryDto.getMaLoai());
        try {
            loaiDanhMuc.setMaLoai(categoryDto.getMaLoai());
            loaiDanhMuc.setTenLoai(categoryDto.getTenLoai());
            loaiDanhMucDao.update(loaiDanhMuc);
            return categoryDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductListCategoryDelete(List<LoaiDanhMucDto> categoryDto) {
        for(int i = 0; i < categoryDto.size(); i++) {
            try {
                loaiDanhMucDao.delete(categoryDto.get(i).getMaLoai());
            }
            catch (Exception ex) {
                return JsonResponseCode.FAILED;
            }
        }
        return JsonResponseCode.SUCCESS;
    }
}
